class Solution {
public:
    int timeRequiredToBuy(vector<int>& t, int k)
    {
        int n = t.size();
        int mx = -0x3f3f3f3f;
        int ans = 0;
        for (auto& a : t) mx = mx > a ? mx : a;
        vector<pair<int, int>> tmp;

        for (int i = 0; i < n; i++)
        {
            if (i == k) tmp.push_back({ t[i], k });
            else tmp.push_back({ t[i], -1 });
        }
        for (int i = 0; i < mx; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (tmp[j].first == 0) continue;
                tmp[j].first--;
                ans++;
                if (tmp[j].second == k && tmp[j].first == 0) return ans;
            }
        }
        return ans;
    }
};